package davel

import (
	"io/ioutil"
	"os"
	"runtime"
	"strings"
)

//获取分割符
func FilePathSep() string {
	return string(os.PathSeparator)
}

//创建目录
func CreateDir(src string) error {
	return os.MkdirAll(src, os.ModePerm)
}

//是否是文件
func IsFile(src string) bool {
	info, err := os.Stat(src)
	return err == nil && !info.IsDir()
}

//是否是目录
func IsFolder(src string) bool {
	info, err := os.Stat(src)
	return err == nil && info.IsDir()
}

//读取文件
func FileRead(filename string) string {
	file, err := os.Open(filename)
	if err != nil {
		return ""
	}
	defer file.Close()
	content, err := ioutil.ReadAll(file)
	if err != nil {
		return ""
	}
	return string(content)
}

//写入文件
func FileWrite(src string, content []byte) bool {
	err := ioutil.WriteFile(src, content, 0777)
	if err!=nil {
		return false
	}
	return true
}

func FileWriteAppend(src string, content []byte) bool {
	file, err := os.OpenFile(src, os.O_APPEND|os.O_CREATE, 0644)
	if err != nil {
		return false
	}
	defer file.Close()
	n, err := file.Write(content)
	if err == nil && n < len(content) {
		return false
	}
	return true
}

//文件是否存在
func FileExists(filename string) bool {
	if _, err := os.Stat(filename); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

//删除文件
func FileDelete(filename string)  {
	filename = FileRealPath(filename)
	if runtime.GOOS == "windows" {
		RunCmd("del /F /S /Q "+filename)
	} else {
		RunCmd("rm -rf "+filename)
	}
}

func FileRealPath(filename string) string {
	news := FilePathSep()
	olds := "/"
	if news=="/" {
		olds = "\\"
	}
	return strings.Replace(filename,olds,news,-1)
}

//获取目录下文件
func GetFileList(src string,filter string,isSrc bool) ([]string, error) {
	var fs []string
	dir, err := ioutil.ReadDir(src)
	if err != nil {
		return nil, err
	}
	var filters []string
	if filter != ""{
		filters = strings.Split(filter,"|")
	}
	for _, v := range dir {
		var appendSrc string
		if isSrc == true{
			appendSrc = src + FilePathSep() + v.Name()
		}else{
			appendSrc = v.Name()
		}
		if v.IsDir() == true {
			fs = append(fs, appendSrc)
			continue
		}
		if filter == "" {
			fs = append(fs, appendSrc)
			continue
		}
		names := strings.Split(v.Name(),".")
		if len(names) == 1{
			fs = append(fs, appendSrc)
			continue
		}
		t := names[len(names)-1]
		for _,filterValue := range filters{
			if t != filterValue{
				continue
			}
			fs = append(fs, appendSrc)
		}
	}
	return fs, nil
}

//获取目录下文件数
func GetFileListCount(src string) (int, error) {
	dir, err := ioutil.ReadDir(src)
	if err != nil {
		return 0, err
	}
	var res int
	for range dir {
		res += 1
	}
	return res, nil
}

//获取文件信息
func GetFileInfo(src string) (os.FileInfo, error) {
	return os.Stat(src)
}

//获取文件大小
func GetFileSize(src string) int64 {
	info, err := os.Stat(src)
	if err != nil {
		return 0
	}
	return info.Size()
}

//读取文件为bytes
func File2Bytes(filename string) ([]byte, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	// FileInfo:
	stats, err := file.Stat()
	if err != nil {
		return nil, err
	}

	// []byte
	data := make([]byte, stats.Size())
	_, err = file.Read(data)
	if err != nil {
		return nil, err
	}
	return data, nil
}